﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformMqttClient
{
    public partial class Form1 : Form
    {
        public string MqttServerIp { get { return ipaddrTextBox.Text; } set { ipaddrTextBox.Text = value; }  }
        public int MqttPort { get { return int.Parse(portTextBox.Text); } set { portTextBox.Text = value.ToString(); } }
        //private string mqttTopic = "test/toipc";
        public string MqttSubTopic { get { return subTopicTextBox.Text; } set { subTopicTextBox.Text = value; } }
        public string MqttPubTopic { get { return pubTopicTextBox.Text; } set { pubTopicTextBox.Text = value; } }
        public string MqttSubMessage { get { return subContentRichTextBox.Text; } set { subContentRichTextBox.Text = value; } }
        public string MqttPubMessage { get { return pubContentRichTextBox.Text; } set { pubContentRichTextBox.Text = value; } }
        public string UserName { get { return userNameTextBox.Text; } set { userNameTextBox.Text = value; } }
        public string Password { get { return passwdTextBox.Text; } set { passwdTextBox.Text = value; } }
        public  string ClientId { get { return clientIdTextBox.Text; } set { clientIdTextBox.Text = value; } }

        private MqttClientService mqttClientService;
        public Form1()
        {
            InitializeComponent();
            mqttClientService = new MqttClientService(this);
        }
        public void ShowSubMessage(string message)
        {
            subContentRichTextBox.Invoke(new Action(() => { subContentRichTextBox.Text = message; }));
        }
        public void ShowLogMessage(string message)
        {
            logRichTextBox.Invoke(new Action(() => { logRichTextBox.Text += message + "\n"; }));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            mqttClientService.MqttClientStart();
        }

        private void Disconnect(object sender, EventArgs e)
        {
            mqttClientService.Disconnect();

        }

        private void PubMessage(object sender, EventArgs e)
        {
            mqttClientService.Publish(MqttPubMessage);
        }
    }
}
